< previous page page_124 next page >

Page 124
Activating a Pop-Up Menu
To have the pop-up menu appear on your screen, you must invoke the form's PopUpMenu method. You do so by specifying the name of the form where the menu will be displayed, the PopUpMenu method, and the name of the menu to be shown. Although you can use this method from anywhere in your code, pop-up menus are used most often in response to mouse clicks, usually those using the right mouse button.
To illustrate this use, make the sample pop-up menu appear when the user right-clicks the form. Of course, in a fully coded application that formats text, the user would probably be right-clicking selected text; however, the following steps illustrate the concept:
1. Open the form's Code window by double-clicking the form.
2. Drop down the Code window's Object box, and select the mnuFormatBold menu item.
3. In mnuFormatBold's Click event procedure, enter the following line of code, which simply causes a message box to appear and report that the procedure was indeed executed:
MsgBox "You just bolded some text!"
4. Drop down the Code window's Object box, and select Form.
5. Drop down the Code window's Procedure box, and select the MouseUp event.
6. Enter the following code into the form's MouseUp event procedure:
If Button = vbRightButton Then
    Form1.PopupMenu mnuFormat
End If
7. Save and run the program. Right-click the form to open the Format menu. Choose the Bold item.
In the code segment you just entered, the MouseUp event is used to take an action whenever a mouse button is pressed. The event passes a parameter, the Button parameter, that tells you which of the mouse buttons was pressed. Because you want the menu to appear in response to only a right button click, you check for the value of the Button parameter. If it is equal to vbRightButton (an intrinsic constant for the right mouse button), the pop-up menu is displayed.
note.gif You can create multiple pop-up menus and have them displayed in response to different mouse buttons or in different areas of the screen. The X and Y parameters passed to the MouseUp event procedure report the location of the mouse cursor at the time the event occurred. square.gif

Using Toolbars in Visual Basic
You have probably noticed that many Windows programs now have one or more toolbars in addition to the menu system. These toolbars provide the users with an easy way to access the most commonly used functions of the program. Some programs also use toolbars to help with

 
< previous page page_124 next page >